package com.jiancong.service.impl;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

@Service
public class SensitiveWordLoader {

    private Set<String> sensitiveWords = new HashSet<>();

    @PostConstruct
    public void loadSensitiveWords() throws IOException {
        // 加载资源文件
        ClassPathResource resource = new ClassPathResource("sensitive_words.txt");
        try (InputStream inputStream = resource.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = reader.readLine()) != null) {
                sensitiveWords.add(line.trim()); // 去除空格并添加到集合中
            }
        }
    }

    public Set<String> getSensitiveWords() {
        return sensitiveWords;
    }

    /**
     * 重新加载敏感词文件
     */
    public void reloadSensitiveWords() throws IOException {
        sensitiveWords.clear(); // 清空现有敏感词
        loadSensitiveWords();   // 重新加载
        System.out.println("敏感词重新加载完成，数量：" + sensitiveWords.size());
    }
}